// 04myCenter/10integral/integral.js
var co = require('../../resource/js/common.js')
var _this, _data
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
     showModal: false,
     userIntegral: '',
     IntegralRecor: '',
     isLook: false,
     userInfo: '',
     gainOrUse: '',
     integralNumbe: '',
     merchantsPassword: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     _this = this
     var user = app.Data.userInfos
     this.setData({ userInfo: user })
     console.log(user.User_ID)
     this.getUserIntegral(user.User_ID);
  },
   //   获取用户积分的数据
   getUserIntegral(id) {
      var param = {
         User_ID: id
      }
      co.getMallApi('user/Fmg/listIntegral', param, function (res) {
         if (res.code == 0) {
            _this.setData({
               userIntegral: res.data
            })
            console.log("【积分数据】", res.data)
         }
      })
   },
   // 获得用户积分明细记录
   getIntegralRecor(id) {
      var param = {
         User_ID: id
      }
      co.getMallApi('user/Fmg/detailIntegral', param, function (res) {
         if (res.code == 0) {
            _this.setData({
               IntegralRecor: res.data
            })
            console.log("【积分明细】", _this.data.IntegralRecor)
         }
      })
   },
   //   打开关闭积分记录列表
   OpenList: function () {
      var isLook = this.data.isLook
      this.setData({
         isLook: !isLook
      })
      if (!this.data.IntegralRecor) {
         this.getIntegralRecor(this.data.userInfo.User_ID)
      }
   },
   toCreditsExchange: function (e) {
      var isConvert = 1
      wx.navigateTo({
         url: '../07giftTaxChange/giftTaxChange?isConvert=' + isConvert
      })
   },
   //   签到
   signIn: function () {
      var param = {
         User_ID: this.data.userInfo.User_ID
      }
      co.getMallApi('user/Fmg/everydaySign', param, function (res) {
         if (res.code == 0) {
            _this.getUserIntegral(_this.data.userInfo.User_ID)
         }
      })
   },
   //   模态框
   //  弹窗
   showDialogBtn: function (e) {
      this.setData({
         showModal: true,
         gainOrUse: e.currentTarget.dataset.id
      })
   },
   // 弹出框蒙层截断touchmove事件
   preventTouchMove: function () {
   },
   //  隐藏模态对话框
   hideModal: function () {
      this.setData({
         showModal: false
      });
   },
   //   获取输入的积分数量
   integralNumber: function (e) {
      var integralNumbe = e.detail.value;
      console.log("【积分数量】", integralNumbe)
      this.setData({ integralNumbe: integralNumbe })
   },
   // 获取输入的商家密码
   merchantsPassword: function (e) {
      var merchantsPassword = e.detail.value;
      console.log("【商家密码", merchantsPassword)
      this.setData({ merchantsPassword: merchantsPassword })
   },
   // 对话框取消按钮点击事件
   onCancel: function () {
      this.hideModal();
   },
   //   对话框确认按钮点击事件
   onConfirm: function () {
      this.hideModal();
      if (this.data.gainOrUse == 1) {
         var param = {
            User_ID: this.data.userInfo.User_ID,
            Record_Integral: this.data.integralNumbe,
            Record_Type: 2,
            Record_Description: '获得积分',
            Operator_Password: this.data.merchantsPassword
         }
         console.log("【param】", param)
         this.getIntegral(param)
      }
   },
   //积分的获得与使用
   getIntegral: function (param) {
      co.getMallApi('user/Fmg/getIntegral', param, function (res) {
         if (res.code == 0) {
            _this.getUserIntegral(_this.data.userInfo.User_ID)
            co.toast("获取积分成功")
            //   app.getWXUserInfo()
            console.log("【用户信息】", _this.data.userInfo)
         }
      })
   },
   onUnload: function () {
      app.getWXUserInfo()
   },
})